package com.kly.cashmall.module.login.presenter;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.kly.cashmall.AppApplication;
import com.kly.cashmall.base.app_action.ActionHeads;
import com.kly.cashmall.base.app_action.BaseActionHelper;
import com.kly.cashmall.bean.BaseEntity;
import com.kly.cashmall.bean.LoginBean;
import com.kly.cashmall.bean.ProtocolBean;
import com.kly.cashmall.framework.mvp.BaseViewPresenter;
import com.kly.cashmall.module.login.LoginActivity;
import com.kly.cashmall.services.Network;
import com.kly.cashmall.services.Params;
import com.kly.cashmall.services.UserInfoHelper;
import com.kly.cashmall.widget.Loading.LoadingDialog;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * @author
 */
public class LoginPresenter extends BaseViewPresenter<LoginViewer> {

    public static final String LINK_URL = "LINK_URL";

    public LoginPresenter(LoginViewer viewer) {
        super(viewer);
    }

    @Override
    public void willDestroy() {

    }


    public void getProtocol(final int type) {
        rxManager.add(Network.getApi().getProtocol().subscribeOn(Schedulers.io()).observeOn
                (AndroidSchedulers.mainThread()).subscribe(new Consumer<ProtocolBean>() {
            @Override
            public void accept(ProtocolBean bean) throws Exception {
                if (getViewer() != null) {
                    getViewer().getProtocolSuccess(type, bean);
                }
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                //返回异常 如404 503，或者返回的数据类型和我们定义的实体对象不一致
            }
        }));
    }

    public void getVerifycodeSend(String mobile) {
        Params params = new Params();
        params.put("mobile", mobile);
        params.put("type", "login");
        rxManager.add(Network.getApi().getVerifycodeSend(params.buildPostBody()).subscribeOn(Schedulers.io()).observeOn
                (AndroidSchedulers.mainThread()).subscribe(new Consumer<BaseEntity>() {
            @Override
            public void accept(BaseEntity bean) throws Exception {
                if (getViewer() != null) {
                    getViewer().getVerifycodeSendSuccess(bean.getMsg());
                }
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                //返回异常 如404 503，或者返回的数据类型和我们定义的实体对象不一致
            }
        }));
    }

    public void getLoginVerifycode(String mobile, String verifycode) {
        LoadingDialog.showLoading(getActivity());
        Params params = new Params();
        params.put("mobile", mobile);
        params.put("verifycode",verifycode);
        rxManager.add(Network.getApi().getLoginVerifycode(params.buildPostBody()).subscribeOn(Schedulers.io()).observeOn
                (AndroidSchedulers.mainThread()).subscribe(new Consumer<LoginBean>() {
            @Override
            public void accept(LoginBean bean) throws Exception {
                LoadingDialog.dismissLoading();
                if (getViewer() != null) {
                    UserInfoHelper.getInstance().setToken(bean.getToken());
                    UserInfoHelper.getInstance().setPhoneNo(bean.getMobile());
                    UserInfoHelper.getInstance().setUserCode(bean.getUserCode());
                    LoginActivity.pFlag = false;
                    afterLoginSuccess();
                }
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                //返回异常 如404 503，或者返回的数据类型和我们定义的实体对象不一致
                LoadingDialog.dismissLoading();
            }
        }));
    }

    /**
     * 登录成功，后处理重定向
     */
    private void afterLoginSuccess() {
        if (getViewer() == null) {
            return;
        }

        final Bundle loginExtraBundle = getViewer().getLoginExtraBundle();
        final String redirectActivityClassName = getViewer().getRedirectActivityClassName();
        final String redirectOtherAction = getViewer().getRedirectOtherAction();

        if (loginExtraBundle == null) {
            if (getViewer() != null) {
                getViewer().setResult();
            }
            BaseActionHelper.with(getActivity()).handleAction(ActionHeads.LMActionHome);
            finish();
            return;
        }
        if (!TextUtils.isEmpty(redirectActivityClassName)) {
            Intent intent = new Intent();
            intent.setComponent(new ComponentName(getActivity(), redirectActivityClassName));
            intent.putExtras(loginExtraBundle);
            getActivity().startActivity(intent);
            finish();
            return;
        }

        if (!TextUtils.isEmpty(redirectOtherAction)) {
            switch (redirectOtherAction) {
                case LINK_URL:
                    BaseActionHelper.with(AppApplication.getInstance())
                            .handleAction(loginExtraBundle.getString(LoginPresenter.LINK_URL));
                    finish();
                    break;
                default:
                    finish();
                    break;
            }
            return;
        }
        finish();
    }

    /**
     * 结束页面
     */
    private void finish() {
        Activity activity = getActivity();
        if (activity != null) {
            activity.finish();
        }
    }
}